package com.sxbbc.common.core.service.impl;

import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.service.redis.RedisUtilsService;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.CCollectPro;
import com.sxbbc.common.core.entity.jz.CCustomer;
import com.sxbbc.common.core.entity.jz.ProcMain;
import com.sxbbc.common.core.enums.ProductStatusEnums;
import com.sxbbc.common.core.enums.ProductTypeEnums;
import com.sxbbc.common.core.mapper.jz.CCollectProMapper;
import com.sxbbc.common.core.service.ICCollectProService;
import com.sxbbc.common.core.service.ICCustomerService;
import com.sxbbc.common.core.service.IProcMainService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.function.common.GetRest;
import com.function.common.RestResponse;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户商品收藏 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-05-16
 */
@Service
public class CCollectProServiceImpl extends ServiceImpl<CCollectProMapper, CCollectPro> implements ICCollectProService {
	@Autowired
	private IProcMainService procMainService;
	@Autowired
	private ICCustomerService customerService;
	@Autowired
	private RedisUtilsService redisUtilsService;
	
	@Override
	public RestResponse handleProCollect(Integer cid, String proCode,Integer collectFlag) {
		boolean handle = false;
		ProcMain procMain = procMainService.selectOneByProCode(proCode);
		if (procMain == null) {
			return GetRest.getFail("获取商品信息失败");
		}
		CalcuUtils calUtil = CalcuUtils.getInstance();
		if (!calUtil.isEquals(ProductStatusEnums.SHELVES.getId(), procMain.getStatus())) {
			return GetRest.getFail("商品已下架");
		}
		CCustomer customer = customerService.getById(cid);
		if (customer == null) {
			return GetRest.getFail("获取用户信息失败");
		}
		QueryWrapper<CCollectPro> wrapper = new QueryWrapper<CCollectPro>();
		wrapper.eq("cid", cid).eq("pro_code", proCode);
        CCollectPro collectPro = super.getOne(wrapper);
        if (collectPro == null) {
            collectPro = new CCollectPro();
            collectPro.setBid(procMain.getBid());
            collectPro.setCid(cid);
            collectPro.setPid(procMain.getId());
            collectPro.setProCode(procMain.getProCode());
            collectPro.setPurl(procMain.getProUrl());
            collectPro.setPname(procMain.getProName());
            collectPro.setDescVal(procMain.getProDesc());
            collectPro.setPrice(procMain.getPrice());
            collectPro.setCreateTime(new Date());
            collectPro.setUnit(procMain.getProUnit());
            handle = super.save(collectPro);
            if (!handle) {
                throw new ThrowJsonException("收藏失败");
            }
            //二级分类不为空 普通商品 缓存到redis中
            if (calUtil.isEquals(procMain.getProType(), ProductTypeEnums.NORMAL.getId())
                    && procMain.getMainClassifySecondId() != null) {
                redisUtilsService.setSecondKey(cid.toString(),procMain.getMainClassifySecondId().toString());
            }
            return GetRest.getSuccess("收藏成功");
        }
        switch (collectFlag){
            case 1:
                collectPro.setStatus(StaticUtils.STATUS_NO);
                handle = super.updateById(collectPro);
                break;
            case 0:
                collectPro.setStatus(StaticUtils.STATUS_YES);
                handle = super.updateById(collectPro);
                break;
            default:
                break;
        }
        if (!handle) {
            throw new ThrowJsonException("处理收藏失败");
        }
		return GetRest.getSuccess("操作成功");
	}

}
